import os
import contextlib
from fastapi import FastAPI
from conf.config import settings
from common.log import log_algo
from controller.router import router
from llmadapter.llmadapter import Adapter
# 创建FastAPI应用实例
app = FastAPI(
    title="TextTransfer API",
    description="文本处理服务API",
    version="1.0.0"
)

# 添加路由
app.include_router(router)

@contextlib.asynccontextmanager
async def lifespan(app: FastAPI):
    """
    服务生命周期管理
    """
    # 启动时执行
    log_algo.info("TextTransfer服务启动中...")
    os.makedirs(settings.FILE_CONFIG['UPLOAD_DIR'], exist_ok=True)
    os.makedirs(settings.FILE_CONFIG['TEMP_DIR'], exist_ok=True)
    os.makedirs(settings.FILE_CONFIG['OUTPUT_DIR'], exist_ok=True)
    log_algo.info("TextTransfer服务启动完成!")
    
    yield
    
    # 关闭时执行（如果需要清理操作）

# 更新 FastAPI 实例以使用 lifespan
app = FastAPI(
    title="TextTransfer API",
    description="文本处理服务API",
    version="1.0.0",
    lifespan=lifespan
)

@app.get("/")
async def root():
    """
    根路由处理
    """
    return {
        "message": "欢迎使用TextTransfer API服务",
        "status": "running"
    }

if __name__ == "__main__":
    import uvicorn
    uvicorn.run(
        app,
        host=settings.API_CONFIG['HOST'],
        port=settings.API_CONFIG['PORT']
    )
